package com.dz.controller.admin;

import com.dz.constant.SystemConstant;
import com.dz.dto.DishDTO;
import com.dz.dto.DishPageQueryDTO;
import com.dz.entity.Dish;
import com.dz.redis.RedisCache;
import com.dz.result.Result;
import com.dz.service.DishService;
import com.dz.vo.DishVO;
import com.dz.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 鄧浩奇
 * @classname DishController
 * @description
 * @since 2023/12/21 19:05
 **/


@Api(tags = "菜品管理接口")
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    private final DishService dishService;

    private final RedisCache redisCache;

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public DishController(DishService dishService, RedisCache redisCache, RabbitTemplate rabbitTemplate) {
        this.dishService = dishService;
        this.redisCache = redisCache;
        this.rabbitTemplate = rabbitTemplate;
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageVo> dishPage(@Valid DishPageQueryDTO queryDTO) {
        PageVo pageVo =  dishService.page(queryDTO);
        return Result.success(pageVo);
    }


    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        DishVO dishVo = dishService.getById(id);
        return Result.success(dishVo);
    }


    @ApiOperation("修改菜品")
    @PutMapping()
    public Result<String> updateById(@RequestBody @Valid DishDTO dishDTO) {
        // 修改
        return dishService.updateById(dishDTO);
    }


    @ApiOperation("新增菜品")
    @PostMapping()
    public Result<String> insertDish(@RequestBody DishDTO dishDTO) {
        return dishService.insert(dishDTO);
    }


    @ApiOperation("根据id删除")
    @DeleteMapping()
    public Result<String> deleteByIds(String ids) {
        List<Long> idList = new ArrayList<>();
        Arrays.stream(ids.split(SystemConstant.SPLIT)).forEach(id -> idList.add(Long.parseLong(id)));
        return dishService.deleteByIds(idList);
    }


    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status, Long id) {
        return dishService.changeStatus(id,status);
    }


    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId) {
        List<Dish> dishes =  dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
    }

}
